#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing

'''
默认管道两侧的连接对象都是双向的（支持发送和接收），一般管道两侧的进程只需要使用其中一个连接对象，
不使用的连接对象应该提前关闭
'''
def adder(*pipe):
    left, right = pipe
    left.close()
    while True:
        try:
            x, y = right.recv()
        except EOFError:
            break
        result = x + y
        right.send(result)
        print("send result: " + str(result))
    print('finished')

if __name__ == "__main__":
    left, right = multiprocessing.Pipe()
    right_p = multiprocessing.Process(target=adder, args=(left,right))
    right_p.start()

    right.close()
    left.send((4,3))
    print("recv result: " + str(left.recv()))

    left.send(('foo','bar'))
    print("recv result: " + str(left.recv()))

    left.close()
    right_p.join()
    print(multiprocessing.active_children())
